package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AnnouncementSupplier;
import com.ruoyi.system.domain.dto.AnnouncementDTO;
import com.ruoyi.system.mapper.AnnouncementSupplierMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AnnouncementMapper;
import com.ruoyi.system.domain.Announcement;
import com.ruoyi.system.service.IAnnouncementService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 公告Service业务层处理
 *
 * @author 协同
 * @date 2022-04-18
 */
@Service
public class AnnouncementServiceImpl implements IAnnouncementService {
    @Resource
    private AnnouncementMapper announcementMapper;
    @Resource
    private AnnouncementSupplierMapper announcementSupplierMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询公告
     *
     * @param id 公告主键
     * @return 公告
     */
    @Override
    public Announcement selectAnnouncementById(Long id) {
        Announcement announcement = announcementMapper.selectAnnouncementById(id);
        AnnouncementSupplier announcementSupplier = new AnnouncementSupplier();
        announcementSupplier.setAid(id);
        List<AnnouncementSupplier> announcementSuppliers = announcementSupplierMapper.selectAnnouncementSupplierList(announcementSupplier);
        List<String> list = new ArrayList<>();
        String names = "";
        if (announcementSuppliers != null) {
            announcementSuppliers.forEach(item -> {
                SysUser sysUser = sysUserMapper.selectUserById(item.getSid());
                list.add(sysUser.getUserName());
            });
        }
        if (list.size() > 0) {
            names = String.join(",", list);
        }
        announcement.setSupplierNames(names);
        return announcement;
    }

    /**
     * 查询企业列表
     *
     * @param announcement 公告
     * @return 公告
     */
    @Override
    public List<Announcement> listEnterprise(Announcement announcement) {
        announcement.setEnterpriseId(SecurityUtils.getLoginUser().getUserId());
        return announcementMapper.selectAnnouncementList(announcement);
    }

    /**
     * 查询供应商列表
     *
     * @param announcement 公告
     * @return 公告
     */
    @Override
    public List<Announcement> listSupplier(Announcement announcement) {
        AnnouncementSupplier announcementSupplier = new AnnouncementSupplier();
        announcementSupplier.setSid(SecurityUtils.getLoginUser().getUserId());
        List<AnnouncementSupplier> announcementSuppliers = announcementSupplierMapper.selectAnnouncementSupplierList(announcementSupplier);
        List<Announcement> list = new ArrayList<>();
        if (announcementSuppliers != null) {
            announcementSuppliers.forEach(item -> {
                Announcement a = announcementMapper.selectAnnouncementById(item.getAid());
                if ("2".equals(a.getStatus())) {
                    list.add(a);
                }
            });
        }
        return list;
    }

    /**
     * 新增公告
     *
     * @param dto 公告
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAnnouncement(AnnouncementDTO dto) {
        Announcement announcement = new Announcement();
        announcement.setEnterpriseId(SecurityUtils.getLoginUser().getUserId());
        announcement.setEmergencyLevel(dto.getEmergencyLevel());
        announcement.setCode(dto.getCode());
        announcement.setTopic(dto.getTopic());
        announcement.setContent(dto.getContent());
        announcement.setStatus("1");
        announcement.setCreateTime(DateUtils.getNowDate());
        announcementMapper.insertAnnouncement(announcement);

        if (dto.getSupplierIds() != null) {
            dto.getSupplierIds().forEach(item -> {
                AnnouncementSupplier announcementSupplier = new AnnouncementSupplier();
                announcementSupplier.setAid(announcement.getId());
                announcementSupplier.setSid(item);
                announcementSupplierMapper.insertAnnouncementSupplier(announcementSupplier);
            });
        }

        return 1;
    }

    /**
     * 修改公告
     *
     * @param announcement 公告
     * @return 结果
     */
    @Override
    public int updateAnnouncement(Announcement announcement) {
        announcement.setUpdateTime(DateUtils.getNowDate());
        return announcementMapper.updateAnnouncement(announcement);
    }

    /**
     * 批量删除公告
     *
     * @param ids 需要删除的公告主键
     * @return 结果
     */
    @Override
    public int deleteAnnouncementByIds(Long[] ids) {
        return announcementMapper.deleteAnnouncementByIds(ids);
    }

    /**
     * 删除公告信息
     *
     * @param id 公告主键
     * @return 结果
     */
    @Override
    public int deleteAnnouncementById(Long id) {
        return announcementMapper.deleteAnnouncementById(id);
    }

    /**
     * 发布公告
     *
     * @param toArray id集合
     * @return 结果
     */
    @Override
    public int updateByIds(Long[] toArray) {
        for (Long id : toArray) {
            Announcement announcement = announcementMapper.selectAnnouncementById(id);
            announcement.setStatus("2");
            announcement.setReleaseTime(DateUtils.getNowDate());
            announcementMapper.updateAnnouncement(announcement);
        }
        return 1;
    }

    /**
     * 删除公告
     *
     * @param toArray ids
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] toArray) {
        for (Long l : toArray) {
            announcementSupplierMapper.deleteAnnouncementSupplierByAid(l);
        }
        return announcementMapper.deleteByIds(toArray);
    }
}
